package com.banshuibao.bsb_android.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.GoBackModel;
import com.banshuibao.bsb_android.model.OrderModel;
import com.banshuibao.bsb_android.util.XToast;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI wxapi;

    private String flag;
    private String order;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wxpay_entry);
        wxapi = WXAPIFactory.createWXAPI(this, Constants.WXID.APP_ID);
        try {
            wxapi.handleIntent(getIntent(), this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        flag = getIntent().getStringExtra("flag");
        order = getIntent().getStringExtra("order");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        wxapi.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (baseResp.errCode == 0) {
                if (!TextUtils.isEmpty(flag)) {
                    switch (flag) {
                        case "1":
                            PostOne();
                            break;
                        case "2":
                            PostTwo();
                            break;
                        case "3":
                            PostThree();
                            break;
                        case "4":
                            PostFour();
                            break;
                        case "5":
                            PostFive();
                            break;
                        case "6":
                            PostSix();
                            break;
                    }
                }
            } else {
                XToast.showToast(this, "支付已取消");
                finish();
            }
        }
    }

    private void PostOne() {
        OrderModel.body body = new OrderModel.body();
        if (TextUtils.isEmpty(order))
            return;
        body.setMchOrderNo(order);
        HttpUtil.doPost(Constants.Url.WX_ORDER, body, new HttpResponse(this, GoBackModel.class) {
            @Override
            public void onResult(Object obj) {
                GoBackModel s = (GoBackModel) obj;
//                LogUtil.e(s);
                XToast.showToast(WXPayEntryActivity.this, s.getDes());
                setResult(RESULT_OK);
                finish();
            }
        });
    }

    private void PostTwo() {
        OrderModel.body body = new OrderModel.body();
        if (TextUtils.isEmpty(order))
            return;
        body.setMchOrderNo(order);
        HttpUtil.doPost(Constants.Url.WX_ORDER, body, new HttpResponse(this, GoBackModel.class) {
            @Override
            public void onResult(Object obj) {
                GoBackModel s = (GoBackModel) obj;
//                LogUtil.e(s);
                XToast.showToast(WXPayEntryActivity.this, s.getDes());
                setResult(RESULT_OK);
                finish();
            }
        });
    }

    private void PostThree() {
        OrderModel.body body = new OrderModel.body();
        if (TextUtils.isEmpty(order))
            return;
        body.setMchOrderNo(order);
        HttpUtil.doPost(Constants.Url.WX_ORDER, body, new HttpResponse(this, GoBackModel.class) {
            @Override
            public void onResult(Object obj) {
                GoBackModel s = (GoBackModel) obj;
//                LogUtil.e(s);
                XToast.showToast(WXPayEntryActivity.this, s.getDes());
                setResult(RESULT_OK);
                finish();
            }
        });
    }

    private void PostFour() {
        OrderModel.body body = new OrderModel.body();
        if (TextUtils.isEmpty(order))
            return;
        body.setMchOrderNo(order);
        HttpUtil.doPost(Constants.Url.WX_ORDER, body, new HttpResponse(this, GoBackModel.class) {
            @Override
            public void onResult(Object obj) {
                GoBackModel s = (GoBackModel) obj;
//                LogUtil.e(s);
                XToast.showToast(WXPayEntryActivity.this, s.getDes());
                setResult(RESULT_OK);
                finish();
            }
        });
    }

    private void PostFive() {
        OrderModel.body body = new OrderModel.body();
        if (TextUtils.isEmpty(order))
            return;
        body.setMchOrderNo(order);
        HttpUtil.doPost(Constants.Url.WX_ORDER, body, new HttpResponse(this, GoBackModel.class) {
            @Override
            public void onResult(Object obj) {
                GoBackModel s = (GoBackModel) obj;
//                LogUtil.e(s);
                XToast.showToast(WXPayEntryActivity.this, s.getDes());
                setResult(RESULT_OK);
                finish();
            }
        });
    }

    private void PostSix() {
        OrderModel.body body = new OrderModel.body();
        if (TextUtils.isEmpty(order))
            return;
        body.setMchOrderNo(order);
        HttpUtil.doPost(Constants.Url.WX_ORDER, body, new HttpResponse(this, GoBackModel.class) {
            @Override
            public void onResult(Object obj) {
                GoBackModel s = (GoBackModel) obj;
//                LogUtil.e(s);
                XToast.showToast(WXPayEntryActivity.this, s.getDes());
                setResult(RESULT_OK);
                finish();
            }
        });
    }
}
